//
//  BCaptchaButton.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/10.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCaptchaButton.h"

#import "BCTimerHandler.h"

@interface BCaptchaButton () <BCTimerHandlerDelegate>

@property (nonatomic, strong) BCTimerHandler *timerHandler;

/**
 *time
 */
@property (nonatomic, assign) NSInteger time;

@end

@implementation BCaptchaButton

- (BCTimerHandler *)timerHandler {
    if (!_timerHandler) {
        _timerHandler = [[BCTimerHandler alloc] init];
        _timerHandler.delegate = self;
    }
    return _timerHandler;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.layer.borderColor = [UIColor qmui_colorWithHexString:@"#C2C2C2"].CGColor;
        self.layer.borderWidth = 1.0;
        self.layer.cornerRadius = 2;
        [self setTitleColor:[UIColor qmui_colorWithHexString:@"#666666"] forState:UIControlStateNormal];
        //
        self.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        self.titleLabel.numberOfLines = 2;
        self.titleLabel.adjustsFontSizeToFitWidth = true;
        self.titleLabel.minimumScaleFactor = 0.6;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.contentEdgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
    }
    return self;
}

- (void)setCaptchaStauts:(BCaptchaButtonStatus)captchaStauts {
    _captchaStauts = captchaStauts;
    [self deallocTimer];
    self.alpha = 1;
    switch (captchaStauts) {
        case BCaptchaButtonStatusNormal:
        {
            [self setTitle:self.sendTitle forState:UIControlStateNormal];
        }
            break;
        case BCaptchaButtonStatusResend:
        {
            [self setTitle:self.resendTitle forState:UIControlStateNormal];
        }
            break;
        case BCaptchaButtonStatusSending:
        {
            [self setTitle:nil forState:UIControlStateNormal];
        }
            break;
        case BCaptchaButtonStatusCountdown:
        {
            self.alpha = 0.5;
            //
            [self setTitle:nil forState:UIControlStateNormal];
            self.time = 120;
            [self timerHandlerRepeatsAction];
            [self.timerHandler start];
        }
            break;
        default:
            break;
    }
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.captchaStauts==BCaptchaButtonStatusCountdown || self.captchaStauts==BCaptchaButtonStatusSending) {
        return false;
    }
    return [super pointInside:point withEvent:event];
}


#pragma mark - BCTimerHandlerDelegate
- (void)timerHandlerRepeatsAction {
    self.time -= 1;
    if (self.time<0) {
        [self deallocTimer];
        self.captchaStauts = BCaptchaButtonStatusResend;
    }else {
        NSString *countdownTitle = [NSString stringWithFormat:@"%@s",@(self.time)];
        [self setTitle:countdownTitle forState:UIControlStateNormal];
    }
}
- (void)deallocTimer {
    if (_timerHandler) {
        [_timerHandler stop];
        _timerHandler = nil;
    }
}



- (void)dealloc {
    [self deallocTimer];
}


+ (CGSize)defaultSize {
    return CGSizeMake(100, 34.0);
}

@end
